<?php
/**
 * Form Json
 *
 * Generates forms by JSON
 *
 * @category    IPB_CMS
 * @package     App_Form
 * @copyright   Copyright (c) 2009 IPB CMS <http://ipb-cms.googlecode.com/>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
 * @author      Henrique Moody <henriquemoody@gmail.com>
 */
class App_Form_Json extends App_Form
{

    /**
     * Decoded JSON
     *
     * @var array
     */
    protected $_decodedJson = array();

    /**
     * Contructor
     *
     * Sets the JSON of the form
     *
     * @param string $json
     * @return void
     */
    public function  __construct($json)
    {
        $this->_setJson($json);
        parent::__construct();
    }

    /**
     * Initialize form
     *
     * @return void
     */
    public function init()
    {
        foreach($this->_decodedJson as $field)
        {
            $this->addElement(
                $field['element'],
                $field['name'],
                $field['options']
            );
        }
        $this->addElement('submit', 'submit', array(
            'label' => 'Send',
            'ignore' => true
        ));
    }

    /**
     * Sets the JSON of the form
     *
     * @param string $json
     */
    protected function _setJson($json)
    {
        if (!is_string($json)) {
            throw new InvalidArgumentException('$json param must be a string');
        }
        $this->_decodedJson = Zend_Json::decode($json);
    }

}
